package a.b.mvc6.controller;

import a.b.mvc6.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<Object> runtimeException (RuntimeException e) {
        log.error("系统出现运行时异常！", e);
        ApiResponse<Object> error = ApiResponse.error(-1, "系统出现运行时异常!");
        return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<Object> httpRequestMethodNotSupportedException (HttpRequestMethodNotSupportedException e) {
        log.error("系统不支持此请求方法！", e);
        ApiResponse<Object> error = ApiResponse.error(-1, "系统不支持此请求方法!");
        return new ResponseEntity<>(error, HttpStatus.METHOD_NOT_ALLOWED);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<Object> noHandlerFoundException (NoHandlerFoundException e) {
        log.error("系统未找到此请求！", e);
        ApiResponse<Object> error = ApiResponse.error(-1, "系统未找到此请求!");
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }


}
